package com.dongdongshop.controller;

import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import com.dongdongsop.data.AjaxResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @DubboReference
    private BrandService brandService;

    //跳转品牌页面
    @RequestMapping("toBrand")
    public String toBrand(){
        return "admin/brand";
    }

    //查询品牌
    @RequestMapping("listPage")
    @ResponseBody
    public List<TbBrand> listPage(){
        List<TbBrand> brandList = brandService.listPage();
        return brandList;
    }

    //跳转品牌添加
    @RequestMapping("toAdd")
    public String toAdd(){
        return "brand/add";
    }

    //品牌添加
    @RequestMapping("add")
    @ResponseBody
    public AjaxResult add(TbBrand brand){
        try {
            int i = brandService.add(brand);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //跳转品牌修改页面，回显
    @RequestMapping("toUpdate")
    public String toUpdate(@RequestParam("id") Long id, Model model){
        TbBrand brand = brandService.toUpdate(id);
        model.addAttribute("brand",brand);
        return "brand/update";
    }

    //品牌修改
    @RequestMapping("update")
    @ResponseBody
    public AjaxResult update(TbBrand brand){
        try {
            int i = brandService.update(brand);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //品牌删除
    @RequestMapping("deleteId")
    @ResponseBody
    public AjaxResult deleteId(@RequestParam("id") Long id){
        try {
            int i = brandService.deleteId(id);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
